package com.ordertracker.persistent.document.template;

import com.ordertracker.persistent.constant.DOCUMENT_TYPE;
import com.ordertracker.persistent.constant.DocumentState;
import com.ordertracker.persistent.dictionary.AuditEntity;
import com.ordertracker.persistent.dictionary.Client;
import com.ordertracker.persistent.dictionary.Organization;
import com.ordertracker.persistent.dictionary.template.OptimisticLockTemplate;
import com.ordertracker.persistent.dictionary.template.StateEntityTemplate;

import javax.persistence.*;

import java.util.Date;

/**
 * Description of class.
 * Created 2/8/13 3:24 PM
 *
 * @author andrey.rodin@playtech.com
 */

@MappedSuperclass
@AttributeOverrides({
        @AttributeOverride(name = "version", column = @Column(name = "version", nullable = false))}
)
public abstract class SimpleDocumentTemplate {

    @Column
    private String externalId;

    @Id
    @GeneratedValue
    private Long id;    
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    private Date date;

    @ManyToOne(optional = false)
    private Organization organization;

    @ManyToOne(optional = false)
    private Client client;

    @Column(length = 255)
    private String comments;

    @Column(nullable = false, length = 1, columnDefinition = "SMALLINT(1)")
    private DOCUMENT_TYPE documentType;

    @Column(nullable = false, length = 1, columnDefinition = "SMALLINT(1)")
    private DocumentState state;

    @Embedded
    private AuditEntity audit;

    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getExternalId() {
        return externalId;
    }

    public void setExternalId(String externalId) {
        this.externalId = externalId;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public DOCUMENT_TYPE getDocumentType() {
        return documentType;
    }

    public void setDocumentType(DOCUMENT_TYPE documentType) {
        this.documentType = documentType;
    }

     public DocumentState getState() {
		return state;
	}

	public void setState(DocumentState state) {
		this.state = state;
	}

	public AuditEntity getAudit() {
		return audit;
	}

	public void setAudit(AuditEntity audit) {
		this.audit = audit;
	}

	@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SimpleDocumentTemplate)) return false;

        SimpleDocumentTemplate that = (SimpleDocumentTemplate) o;

        if (!client.equals(that.client)) return false;
        if (comments != null ? !comments.equals(that.comments) : that.comments != null) return false;
        if (!date.equals(that.date)) return false;
        if (documentType != that.documentType) return false;
        if (externalId != null ? !externalId.equals(that.externalId) : that.externalId != null) return false;
        if (!organization.equals(that.organization)) return false;
        if (state != that.state) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = externalId != null ? externalId.hashCode() : 0;
        result = 31 * result + date.hashCode();
        result = 31 * result + organization.hashCode();
        result = 31 * result + client.hashCode();
        result = 31 * result + (comments != null ? comments.hashCode() : 0);
        result = 31 * result + documentType.hashCode();
        result = 31 * result + state.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return
                " externalId=" + externalId +
                ", date=" + date +
                ", organization=" + organization +
                ", client=" + client +
                ", comments='" + comments + '\'' +
                ", documentType=" + documentType +
                ", state=" + state +
                "} ";
    }
}
